package com.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.common.result.RPage;
import com.blog.entity.po.Article;
import com.blog.entity.po.Category;
import com.blog.mapper.ArticleMapper;
import com.blog.mapper.CategoryMapper;
import com.blog.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-03-24
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public RPage<Category> getListByPage(Integer currentPage, Integer pageSize) {
        if (currentPage == null || currentPage == 0) {
            currentPage = 1;
        }
        Page<Category> page = Page.of(currentPage, pageSize);
        lambdaQuery()
                .orderByDesc(Category::getUpdateTime)
                .page(page);
        RPage<Category> result = new RPage<>();
        result.setTotal(page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public void delById(String id) {
        Category one = lambdaQuery()
                .eq(Category::getId, id)
                .one();
        if (Objects.isNull(one)) {
            throw new RuntimeException("分类不存在");
        }
        LambdaQueryWrapper<Article> lqwArticle = new LambdaQueryWrapper<>();
        lqwArticle.eq(Article::getCatId, id);
        List<Article> articles = articleMapper.selectList(lqwArticle);
        if (!articles.isEmpty()) {
            throw new RuntimeException("分类下面还存在分类");
        }
        boolean b = removeById(id);
        if (!b) {
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public void saveCategory(Category category) {
        Category one = lambdaQuery()
                .eq(Category::getCatName, category.getCatName())
                .one();
        if (!Objects.isNull(one)) {
            throw new RuntimeException("分类名称已存在");
        }
        category.setStatus(1);
        boolean b = save(category);
        if (!b) {
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    public void editCategory(Category category) {

        Category one = lambdaQuery()
                .eq(Category::getCatName, category.getCatName())
                .one();

        if (!Objects.isNull(one) && !Objects.equals(one.getId(), category.getId())) {
            throw new RuntimeException("分类名称已存在");
        }

        boolean b = updateById(category);
        if (!b) {
            throw new RuntimeException("修改失败");
        }


    }
}
